import java.awt.event.*;
import java.awt.*;

public class ActionControl extends Panel {

    // Car data source
    private Core carDisplay;
    
    public ActionControl(Core carDisplay) {
        this.carDisplay = carDisplay;
        
        Button goToInitial = new Button("Go To Initial");
        goToInitial.addActionListener(new GoToInitialListener());
        this.add(goToInitial);

        Button toggleAction = new Button("  Start  ");
        toggleAction.addActionListener(new ToggleActionListener(toggleAction));
        this.add(toggleAction);
    }

    private class GoToInitialListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            carDisplay.resetToInitial();
        }
    }

    private class ToggleActionListener implements ActionListener {
        private String[] labels = {"  Pause  ", "Unpause"};
        private int index = 0;
        private Button button;
        public ToggleActionListener(Button b) {
            this.button = b;
        }
        public void actionPerformed(ActionEvent e) {
            // toggle it
            carDisplay.togglePaused();

            // change label
            button.setLabel( labels[index] );

            // update index
            ++index;
            index %= 2;
        }
    }
    
}
